/**
 * 修改其他要求
 */
<template>
	<vp-dialog ref="editDialogRef" :span="1">
		<template #title>
			<vp-label-add v-if="type === 'add'"/>
        	<vp-label-edit v-else/>
		</template>
		<el-form v-loading="loading" :model="formData" :rules="formRules" ref="formRef" label-width="130px">
			<el-row>
				<vp-col :span="1">
					<el-form-item prop="qtyq">
						<template #label><vp-label zh="其他要求" en="Other Requirement" align="left"/></template>
						<el-input type="textarea" v-model="formData.qtyq" v-trim maxlength="1000" rows="5"></el-input>
					</el-form-item>
				</vp-col>
			</el-row>
		</el-form>
		<template #footer>
			<vp-btn-confirm :loading="saveLoading" @click="handleSave"/>
		</template>
	</vp-dialog>
</template>
<script setup lang="ts" name="editOtherRequirement">
import { ref } from 'vue'
import request from '/@/utils/request';
import validateUtils from '/@/utils/validateUtils';

	const emits = defineEmits(['formSubmited']);

	const formData = ref({
        id: "",
		qtyq: "",
		mainid: "" //岗位Id
    });

	const formRules = {
        qtyq: [ validateUtils.notNull ]
    }

	const loading = ref(false);
    const saveLoading = ref(false);
    const type = ref('add');

	const editDialogRef = ref();
	const formRef = ref();

	// 打开弹窗
	const handleOpen = (param: any) => {
		initFormData();
		type.value = param.type;
		formData.value.mainid = param.mainid;
		editDialogRef.value.handleOpen();
		if (param.type === 'edit') {
			loading.value = true;
			request.get(`/training/position/otherRequirement/${param.id}`).then((res) => {
				formData.value = {
					...formData.value,
					...res.data
				};
				loading.value = false;
			})
		}
	};

	// 清空数据
	const initFormData = () => {
		formData.value = {
			id: "",
			qtyq: "",
			mainid: "" //岗位Id
		}
	}

	// 保存信息
	const handleSave = () => {
		if (loading.value) {
			return;
		}
		saveLoading.value = true;
		formRef.value.validate((valid: Boolean) => {
			if (valid) {
				request.post("/training/position/otherRequirement/save", {
					...formData.value
				}).then(() => {
					emits("formSubmited");
					editDialogRef.value.handleClose();							
				}).catch(() => {
				}).finally(()=>{
					saveLoading.value = false;
				})
			} else {
				saveLoading.value = false;
			}
		})
	}

	defineExpose({
        handleOpen,
    })
</script>
